import { Request, Response } from "express";
import { responseError, responseSuccess } from "../../common/response";
import RoleModel from "../../models/role/index";
import { Op } from "sequelize";

class RoleController {
  //#region 角色列表
  async getRoleList(req: Request, res: Response) {
    const { page, pageSize, keyword } = req.query;
    const offset = (Number(page) - 1) * Number(pageSize);
    const size = Number(pageSize);
    const { count, rows } = await RoleModel.findAndCountAll({
      offset: offset,
      limit: size,
      where: {
        [Op.or]: [
          {
            users: {
              [Op.like]: `%${keyword}%`,
            },
          },
          {
            name: {
              [Op.like]: `%${keyword}%`,
            },
          },
        ],
      },
    });
    const data = {
      total: count,
      data: rows,
      page: Number(page),
      pageSize: Number(pageSize),
    };
    res.json(responseSuccess(data));
  }
  //#endregion

  //#region 添加角色
  async addRole(req: Request, res: Response) {
    const { body } = req;
    const [created] = await RoleModel.findOrCreate({
      where: {
        name: body.name,
      },
      defaults: body,
    });
    if (!created) return res.json(responseError("角色已存在"));
    res.json(responseSuccess("添加成功"));
  }
  //#endregion

  //#region 删除角色
  async deleteRole(req: Request, res: Response) {
    const { id } = req.body;
    const rows = await RoleModel.destroy({
      where: {
        id,
      },
    });
    if (rows === 0) return res.json(responseError("删除失败"));
    res.json(responseSuccess("删除成功"));
  }
  //#endregion

  //#region 修改角色
  async changeRoleInfo(req: Request, res: Response) {
    const rows = await RoleModel.update(
      {
        name: req.body.name,
        remark: req.body.remark,
      },
      {
        where: {
          id: req.body.id,
        },
      },
    );
    if (rows[0] === 0) return res.json(responseError("修改失败"));
    res.json(responseSuccess("修改成功"));
  }
  //#endregion

  //#region 修改角色状态
  async changeRoleStatus(req: Request, res: Response) {
    const { id, status } = req.body;
    const rows = await RoleModel.update(
      {
        status,
      },
      {
        where: {
          id,
        },
      },
    );
    if (rows[0] === 0) return res.json(responseError("修改失败"));
    res.json(responseSuccess("修改成功"));
  }
  //#endregion

  // #region 获取角色信息
  async getRoleInfo(req: Request, res: Response) {
    const { id } = req.query;
    const role = await RoleModel.findOne({
      where: {
        id,
      },
    });
    if (role === null) return res.json(responseError("角色不存在"));
    if (role.status == 0) return res.json(responseError("角色已禁用"));
    res.json(responseSuccess(role));
  }
  //#endregion

  // #region 保存角色权限
  async saveRoleAuth(req: Request, res: Response) {
    const { id, menus, buttons } = req.body;
    const rows = await RoleModel.update(
      {
        menus: menus,
        buttons: buttons,
      },
      {
        where: {
          id,
        },
      },
    );
    if (rows[0] === 0) return res.json(responseError("保存失败"));

    res.json(responseSuccess("保存成功"));
  }
  //#endregion

  // #region 保存绑定用户
  async saveRoleUser(req: Request, res: Response) {
    const { id, users } = req.body;
    const rows = await RoleModel.update(
      {
        users: users,
      },
      {
        where: {
          id,
        },
      },
    );
    if (rows[0] === 0) return res.json(responseError("保存失败"));
    res.json(responseSuccess("保存成功"));
  }
  //#endregion
}

export default RoleController;
